{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 总结心得（Python:你知道些什么？）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 字典   \n",
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " ### 自我评分:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'IDLE': ['操作，90', '意义:80'],\n",
       " 'jupyter': ['操作：80', '意义：80'],\n",
       " 'pycharm': ['操作：85', '意义：90']}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_base = {\n",
    "        \"IDLE\":[\"操作，90\",\"意义:80\"],\n",
    "        \"jupyter\":[\"操作：80\",\"意义：80\"],\n",
    "        \"pycharm\":[\"操作：85\",\"意义：90\"],\n",
    "}\n",
    "python_base\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  基本知识：      \n",
    "   * 字典是“键值对”的无序可变序列，字典中的每个元素都是一个“键值对”    \n",
    "   * dict是无序的；跟多变的 value 不同的是 key 具有唯一性；key 的数据类型必须是固定的不可变的，如：数字、字符串、元组等，而 value 就没有那么多的要求可以为任意的Python数据类型。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建字典的方式有3种\n",
    "* 通过{}，dict()创建字典对象 形式为：f = dict()，f = {}       \n",
    "* zip()创建，形式为：k=['name','age']，v =['zhangsan',20,'teacher']，d=dict(zip(k,v))      \n",
    "* fromkeys()创建，  形式为：  a=dict.fromkeys(['name','age','job'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字典元素访问  \n",
    " * 通过 [键] 获得“值”。若键不存在，则抛出异常"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'zhangshang'"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = {\"name\":\"zhangshang\",\"age\":18,'job':'student'}\n",
    "a['name']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'school'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-26-5eb781a76e49>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'school'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m: 'school'"
     ]
    }
   ],
   "source": [
    "a['school']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字典增加、修改、删除"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 新增和修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'zhangshang', 'school': '中山大学南方学院', 'age': ' 20'}"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a['age'] = \" 20\"\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'zhangshang', 'school': '中山大学南方学院', 'age': ' 20'}"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a['school']='中山大学南方学院'\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 将新字典中所有键值对全部添加到旧字典对象上，如果key有重复，则直 接覆盖     \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'zhangshang', 'school': '中山大学南方学院', 'age': ' 20', 'hair': 'yellow'}\n"
     ]
    }
   ],
   "source": [
    "b=a\n",
    "b['hair']='yellow'\n",
    "print(b)\n",
    "a.update(b)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 删除元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'zhangshang', 'school': '中山大学南方学院', 'hair': 'yellow'}"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "del a['age']\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 删除指定 键值对，并返回对应的“值对象”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'yellow'"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.pop('hair')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 随机删除和返回该键值对。字典是“无序可变序列”，因此没有第一个元 素、最后一个元素的概念；popitem 弹出随机的项，因为字典并没有\"最后的元素\"或者其 他有关顺序的概念。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('school', '中山大学南方学院')"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.popitem()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 删除所有键值对"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{}"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.clear()\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字典的嵌套"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'薛之谦': {'年龄': '28', '性别': '男', '身份': '歌手'},\n",
       " '周杰伦': {'年龄': '32', '性别': '男', '身份': '歌手'}}"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qiantao_Dict = {\n",
    "    \"薛之谦\":{\n",
    "        \"年龄\":\"28\",\n",
    "        \"性别\":\"男\",\n",
    "        \"身份\":\"歌手\",\n",
    "    },\n",
    "    \"周杰伦\":{\n",
    "        \"年龄\":\"32\",\n",
    "        \"性别\":\"男\",\n",
    "        \"身份\":\"歌手\",\n",
    "    }\n",
    "}\n",
    "qiantao_Dict"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 迭代处理字典\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*  for循环迭代处理字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': 0, 'e': 0, 'i': 0, 'o': 0, 'u': 0}\n",
      "a\n",
      "e\n",
      "i\n",
      "o\n",
      "u\n"
     ]
    }
   ],
   "source": [
    "found = {}\n",
    "found [\"a\"]= 0\n",
    "found [\"e\"]= 0\n",
    "found [\"i\"]= 0\n",
    "found [\"o\"]= 0\n",
    "found [\"u\"]= 0\n",
    "print(found)\n",
    "for kv in found:\n",
    "    print(kv)  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* range循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "H\n",
      "i\n",
      "!\n",
      "Head First Rocks!\n",
      "Head First Rocks!\n",
      "Head First Rocks!\n",
      "Head First Rocks!\n",
      "Head First Rocks!\n"
     ]
    }
   ],
   "source": [
    "for i in [1,2,3,]:\n",
    "       print(i)\n",
    "for ch in \"Hi!\":\n",
    "\tprint(ch)\n",
    "for num in range (5):\n",
    "\tprint(\"Head First Rocks!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "code_folding": [
     3
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入名字俊杰\n",
      "wonderful\n"
     ]
    }
   ],
   "source": [
    "input(\"请输入名字\")\n",
    "import random\n",
    "a=random.randint(1,5)\n",
    "if a==5:\n",
    "    print(\"happy\")\n",
    "elif a==4:\n",
    "    print(\"joyful\")\n",
    "elif a==3:\n",
    "    print(\"wonderful\")\n",
    "elif a==2:\n",
    "    print(\"charming\")\n",
    "else:\n",
    "    print(\"enjoy your day\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 字典频度计数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0}\n",
      "{'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 1}\n",
      "{'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 2}\n"
     ]
    }
   ],
   "source": [
    "found = {}\n",
    "found [\"a\"]= 0\n",
    "found [\"b\"]= 0\n",
    "found [\"c\"]= 0\n",
    "found [\"d\"]= 0\n",
    "found [\"e\"]= 0\n",
    "print(found)\n",
    "found [\"e\"] = found [\"e\"]+1\n",
    "print(found)\n",
    "found[\"e\"] +=1\n",
    "print(found)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 频度计数磁贴"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "provide a word to search for vowels: ae\n",
      "a was found 1 time(s).\n",
      "e was found 1 time(s).\n",
      "i was found 0 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 0 time(s).\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = input(\"provide a word to search for vowels: \")\n",
    "found = {}\n",
    "found ['a'] = 0\n",
    "found ['e'] = 0\n",
    "found ['i'] = 0\n",
    "found ['o'] = 0\n",
    "found ['u'] = 0\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found[letter]+=1\n",
    "for (k,v) in sorted(found.items()):\n",
    "    print(k,'was found',v,'time(s).') \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字典中 in和not in的用法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 用in检查字典是否存在某个键"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'apples': 10, 'bananas': 1}\n"
     ]
    }
   ],
   "source": [
    "fruits = {}\n",
    "fruits['apples']=10\n",
    "if 'bananas' in fruits:\n",
    "    fruits['bananas'] += 1\n",
    "else:\n",
    "    fruits['bananas'] =1\n",
    "print(fruits)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 用not in 检查"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'apples': 10, 'bananas': 1}\n"
     ]
    }
   ],
   "source": [
    "fruits = {}\n",
    "fruits['apples']=10\n",
    "if 'bananas'not in fruits:\n",
    "    fruits['bananas'] =0\n",
    "fruits['bananas']+=1\n",
    "print(fruits)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 使用setdefault"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "provide a word to search for vowels:ae\n",
      "a was found 1 time(s).\n",
      "e was found 1 time(s).\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = input(\"provide a word to search for vowels:\")\n",
    "found = {}\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found.setdefault(letter,0)\n",
    "        found[letter]+=1\n",
    "\n",
    "for k,v in sorted(found.items()):\n",
    "    print(k,\"was found\",v,\"time(s).\") "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 集合"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 自我评分:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'IDLE': ['操作，８0', '意义:８０'],\n",
       " 'jupyter': ['操作：8５', '意义：80'],\n",
       " 'pycharm': ['操作：8０', '意义：７９']}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_base = {\n",
    "        \"IDLE\":[\"操作，８0\",\"意义:８０\"],\n",
    "        \"jupyter\":[\"操作：8５\",\"意义：80\"],\n",
    "        \"pycharm\":[\"操作：8０\",\"意义：７９\"],\n",
    "}\n",
    "python_base\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  基本知识： \n",
    ">集合是无序可变，元素不能重复。实际上，集合底层是字典实现，集合的所有元素都是字典 中的“键对象”，因此是不能重复的且唯一的。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建和add()增加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3, 4, 5}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{1, 2, 3, 4, 5, 6}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = {1,2,3,4,5,4}      # 相同元素会自动合并\n",
    "print(a)\n",
    "a.add(6)\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### set()，将列表、元组等可迭代对象转成集合。如果原来数据存在重复数据，则只保 留一个。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3, 4, 5}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a= [1,2,2,3,4,5]\n",
    "b=set(a)\n",
    "b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### remove()删除指定元素，clear()清空整个集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 3, 4, 5}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.remove(2)\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "set()"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.clear()\n",
    "b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 集合的并集、交集、差集等运算："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* union合并集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 'good'}\n",
      "{4, 5, 6}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{1, 2, 4, 5, 6, 'good'}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a ={1,2,\"good\"}\n",
    "b={4,5,6}\n",
    "print(a)\n",
    "print(b)\n",
    "c=a.union(b)\n",
    "c"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*  difference()差集：告诉你哪些不是共有的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 'good'}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d=c.difference(b)\n",
    "d"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* intersection()差集：报告共同对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 'good'}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f=a.intersection(c)\n",
    "f"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 列表   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 自我评分:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'IDLE': ['操作，82', '意义:85'],\n",
       " 'jupyter': ['操作：84', '意义：80'],\n",
       " 'pycharm': ['操作：8０', '意义：80']}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_base = {\n",
    "        \"IDLE\":[\"操作，82\",\"意义:85\"],\n",
    "        \"jupyter\":[\"操作：84\",\"意义：80\"],\n",
    "        \"pycharm\":[\"操作：8０\",\"意义：80\"],\n",
    "}\n",
    "python_base\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  基本知识：      \n",
    "   * 列表是Python中内置有序、可变序列，列表的所有元素放在一对中括号“[]”中，并使用逗号分隔开；\n",
    "   * 当列表元素增加或删除时，列表对象自动进行扩展或收缩内存，保证元素之间没有缝隙；\n",
    "   * 在Python中，一个列表中的数据类型可以各不相同，可以同时分别为整数、实数、字符串等基本类型，甚至是列表、元组、字典、集合以及其他自定义类型的对象。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 列表创建与删除"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 使用“=”直接将一个列表赋值给变量即可创建列表对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'b', 'mpilgrim', 'z', 'example']"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_list = ['a', 'b', 'mpilgrim', 'z', 'example']\n",
    "a_list"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 也可以使用list()函数将元组、range对象、字符串或其他类型的可迭代对象类型的数据转换为列表。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3, 5, 7, 9, 11]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_list = list((3,5,7,9,11))\n",
    "a_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3, 5, 7, 9]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(1,10,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list('hello world')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 当不再使用时，使用del命令删除整个列表，如果列表对象所指向的值不再有其他对象指向，Python将同时删除该值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'a_list' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-29-85b76117effa>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;32mdel\u001b[0m \u001b[0ma_list\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0ma_list\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'a_list' is not defined"
     ]
    }
   ],
   "source": [
    "del a_list\n",
    "a_list"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 列表常用方法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "方法||说明\n",
    "---|:--:|:---:\n",
    " lst.append(x)||将元素x添加至列表lst尾部\n",
    " lst.extend(L)||将列表L中所有元素添加至列表lst尾部\n",
    " lst.insert(index, x)|| 在列表lst指定位置index处添加元素x，该位置后面的所有元素后移一个位置\n",
    " lst.remove(x)|| 在列表lst中删除首次出现的指定元素，该元素之后的所有元素前移一个位置\n",
    " lst.pop([index])||删除并返回列表lst中下标为index（默认为-1）的元素\n",
    " lst.clear()||删除列表lst中所有元素，但保留列表对象\n",
    " lst.index(x)|| 返回列表lst中第一个值为x的元素的下标，若不存在值为x的元素则抛出异常\n",
    " lst.count(x)||返回指定元素x在列表lst中的出现次数\n",
    " lst.reverse()||对列表lst所有元素进行逆序\n",
    " lst.sort(key=None, reverse=False)|| 对列表lst中的元素进行排序，key用来指定排序依据，reverse决定升序（False），还是降序（True）\n",
    " lst.copy()||返回列表lst的浅复制\n",
    " \"+\"||可以使用“+”运算符将元素添加到列表中"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 列表元素访问与计数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 使用下标直接访问列表元素，如果指定下标不存在，则抛出异常。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "alist=[1,2,3,4,5,6]\n",
    "print(alist)\n",
    "alist[3] # 从索引值为0开始数，所以索引值第3位为4，得出的结果就为4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 7, 5, 6]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "alist[3]=7 #替换列表值\n",
    "alist"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 使用列表对象的index()方法获取指定元素首次出现的下标，若列表对象中不存在指定元素，则抛出异常。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "alist\n",
    "alist.index(7) #7位于索引值第3位,得出的结果就为3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 使用列表对象的count()方法统计指定元素在列表对象中出现的次数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "alist\n",
    "alist.count(2) # 2出现的次数为1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 切片操作\n",
    ">切片使用2个冒号分隔的3个数字来完成：    \n",
    "第一个数字表示切片开始位置（默认为0）。    \n",
    "第二个数字表示切片截止（但不包含）位置（默认为列表长度）。    \n",
    "第三个数字表示切片的步长（默认为1），当步长省略时可以顺便省略最后一个冒号。       \n",
    "切片操作不会因为下标越界而抛出异常，而是简单地在列表尾部截断或者返回一个空列表，代码具有更强的健壮性。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 切片训练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3, 5, 7, 8, 12, 11, 13, 14, 9]"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "aList = [1, 3, 5, 7, 8, 12, 11, 13, 14, 9]\n",
    "aList[::]    #返回包含所有元素的新列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 5, 8, 11, 14]"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "aList[::-1]  #逆序的所有元素  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 5, 8, 11, 14]"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "aList[::2]   #偶数位置，隔一个取一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3, 7, 12, 13, 9]"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "aList[1::2] #奇数位置，隔一个取一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[7, 8, 12, 11, 13, 14, 9]"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "aList[3::] #从下标3开始的所有元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[7, 8, 12]"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "aList[3:6]   #下标在[3, 6)之间的所有元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3, 5, 7, 8, 12, 11, 13, 14, 9]"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "aList[0:100:1] #前100个元素，自动截断"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "aList[100:]  #下标100之后的所有元素，自动截断"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 可以使用切片来原地修改列表内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3, 5, 7, 9]"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "aList = [3, 5, 7]\n",
    "aList[len(aList):] = [9]                       #在尾部追加元素\n",
    "aList"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 9]"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "aList[:3] = [1, 2, 3]                            #替换前3个元素\n",
    "aList"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[9]"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "aList[:3] = []                                        #删除前3个元素\n",
    "aList"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "aList = list(range(10))\n",
    "aList"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 0, 3, 0, 5, 0, 7, 0, 9]"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "aList[::2] = [0]*5                                  #替换偶数位置上的元素\n",
    "aList"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 使用del与切片结合来删除列表元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[9, 11]"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "aList = [3,5,7,9,11]\n",
    "del aList[:3]                                    #删除前3个元素\n",
    "aList"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 用于序列操作的常用内置函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> len(列表)：返回列表中的元素个数，同样适用于元组、字典、集合、字符串等。     \n",
    "max(列表)、 min(列表)：返回列表中的最大或最小元素，同样适用于元组、字典、集合、range对象等。     \n",
    "sum(列表)：对列表的元素进行求和运算，对非数值型列表运算需要指定start参数，同样适用于元组、range。      "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "55"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(range(1, 11))  #sum()函数的start参数默认为0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* zip()函数返回可迭代的zip对象。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<zip at 0x1a7eb24a0c0>"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "aList = [1, 2, 3]\n",
    "bList = [4, 5, 6]\n",
    "cList = zip(a, b)    #返回zip对象\n",
    "cList"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(1, 4), (2, 5), ('good', 6)]"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(cList) #把zip对象转换成列表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* enumerate(列表):枚举列表元素，返回枚举对象，其中每个元素为包含下标和值的元组。该函数对元组、字符串同样有效。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, 'a')\n",
      "(1, 'b')\n",
      "(2, 'c')\n",
      "(3, 'd')\n",
      "(4, 'e')\n",
      "(5, 'f')\n"
     ]
    }
   ],
   "source": [
    " for item in enumerate('abcdef'):\n",
    "           print(item)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 元组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Python的元组与列表类似，不同之处在于元组的元素不能修改,元组使用小括号,列表使用方括号,元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "code_folding": []
   },
   "source": [
    "### 自我评分:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'IDLE': ['操作，85', '意义:85'],\n",
       " 'jupyter': ['操作：90', '意义：85'],\n",
       " 'pycharm': ['操作：85', '意义：80']}"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_base = {\n",
    "        \"IDLE\":[\"操作，85\",\"意义:85\"],\n",
    "        \"jupyter\":[\"操作：90\",\"意义：85\"],\n",
    "        \"pycharm\":[\"操作：85\",\"意义：80\"],\n",
    "}\n",
    "python_base"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('physics', 'chemistry', 1997, 2000)"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tup1 = ('physics', 'chemistry', 1997, 2000)\n",
    "tup1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 访问元组\n",
    "> 元组与字符串类似，下标索引从0开始，可以进行截取，组合等"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 元组可以使用下标索引来访问元组中的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'physics'"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tup1[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 修改元组\n",
    "> 元组中的元素值是不允许修改的，但我们可以对元组进行连接组合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(12, 34.56, 'abc', 'xyz')"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tup1 = (12, 34.56);\n",
    "tup2 = ('abc', 'xyz');\n",
    "tup3 = tup1 + tup2;\n",
    "tup3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 删除元组\n",
    "> 元组中的元素值是不允许删除的，但我们可以使用del语句来删除整个元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('physics', 'chemistry', 1997, 2000)\n",
      "After deleting tup : \n"
     ]
    },
    {
     "ename": "NameError",
     "evalue": "name 'tup' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-66-8c6c8c2758e9>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;32mdel\u001b[0m \u001b[0mtup\u001b[0m\u001b[1;33m;\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m\"After deleting tup : \"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[0mtup\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'tup' is not defined"
     ]
    }
   ],
   "source": [
    "tup = ('physics', 'chemistry', 1997, 2000);\n",
    "print(tup);\n",
    "del tup;\n",
    "print (\"After deleting tup : \")\n",
    "tup"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 元组运算符\n",
    "> 与字符串一样，元组之间可以使用 + 号和 * 号进行运算。这就意味着他们可以组合和复制，运算后会生成一个新的元组。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 元组索引，截取\n",
    "> 因为元组也是一个序列，所以我们可以访问元组中的指定位置的元素，也可以截取索引中的一段元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [],
   "source": [
    "L = ('spam', 'Spam', 'SPAM!')  # 示例代码"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 元组内置函数      \n",
    "> Python元组包含了以下内置函数    \n",
    "1、cmp(tuple1, tuple2)：比较两个元组元素。    \n",
    "2、len(tuple)：计算元组元素个数。   \n",
    "3、max(tuple)：返回元组中元素最大值。   \n",
    "4、min(tuple)：返回元组中元素最小值。   \n",
    "5、tuple(seq)：将列表转换为元组。    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 函数与模块\n",
    "> 1.python的程序由包(package)、模块(module)和函数组成。      \n",
    "2.模块是处理某一类问题的集合，模块由函数和类组成。    \n",
    "3.包是由一系列模块组成的集合"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 自我评分:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'IDLE': ['操作，82', '意义:85'],\n",
       " 'jupyter': ['操作：90', '意义：90'],\n",
       " 'pycharm': ['操作：80', '意义：80']}"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_base = {\n",
    "        \"IDLE\":[\"操作，82\",\"意义:85\"],\n",
    "        \"jupyter\":[\"操作：90\",\"意义：90\"],\n",
    "        \"pycharm\":[\"操作：80\",\"意义：80\"],\n",
    "}\n",
    "python_base"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 函数\n",
    "> 1.在Python中可以使用def关键字来定义函数，和变量一样每个函数也有一个响亮的名字，而且命名规则跟变量的命名规则是一致的；   \n",
    "2.在函数名后面的圆括号中可以放置传递给函数的参数，这一点和数学上的函数非常相似，程序中函数的参数就相当于是数学上说的函数的自变量 ；    \n",
    "3.函数执行完成后我们可以通过return关键字来返回一个值，这相当于数学上说的函数的因变量。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 示例：相加函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [],
   "source": [
    "def xiangjia(a,b): #a,b->str\n",
    "    return a+b # 返回值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xiangjia(3,4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 参数传递"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ab\n",
      "ba\n"
     ]
    }
   ],
   "source": [
    "def add(a,b):\n",
    "    return a+b\n",
    "print(add('a','b')) #顺序依次传递\n",
    "print(add(b='a',a='b')) #按形参赋值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### python 允许函数内部嵌套函数\n",
    "> 但是内部的函数只允许内部使用"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### lambda函数:定义一些简单的函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "add=lambda a,b:a+b\n",
    "print(add(1,2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 函数列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "25\n"
     ]
    }
   ],
   "source": [
    "add=lambda a,b:a+b\n",
    "def solve(a):\n",
    "    return a*a\n",
    "d=[add,solve]\n",
    "print(d[0](1,2))\n",
    "print(d[1](5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#####   函数字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "25\n"
     ]
    }
   ],
   "source": [
    "add=lambda a,b:a+b\n",
    "def solve(a):\n",
    "    return a*a\n",
    "d={'求和':add,'平方':solve}\n",
    "print(d['求和'](1,2))\n",
    "print(d['平方'](5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 递归函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 递归函数可以在函数主体内直接或间接地调用自己，即函数的嵌套是函数本身。    \n",
    "* 递归是一种程序设计方法，使用递归可以减少重复的代码，使程序变得简洁。    \n",
    "* 递归的过程分为两个阶段：递推和回归。     \n",
    "* 递归函数的原理：    \n",
    "  第一阶段，递归函数在内部调用自己。每一次函数调用又重新开始执行此函数的代码，直到某一级递归程序结束。     \n",
    "  第二阶段，递归函数从后往前返回。递归函数从最后一级开始返回，一直返回到第一次调用的函数体内。即递归函数逐级调用完毕后，再按相反的顺序逐级返回。     "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 递归的实现："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 一个问题能否用递归实现，看其是否具有下面的特点：  \n",
    "* 需有完成任务的递推公式。   \n",
    "* 结束递归的条件。   \n",
    "* 编写递归函数时，程序中必须有相应的语句：    \n",
    "* 一个递归调用语句。   \n",
    "* 测试结束语句。先测试，后递归调用。    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 递归的评价与消除递归"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 递归程序虽然易读、易编，但需要占用额外的内存空间，并且执行速度也受影响。\n",
    "* 是否利用递归编程要看实际问题，如果要节约内存就用循环语句实现。若对内存要求并不高，可以用递归编程。\n",
    "* 如果不用递归程序很难实现，则只能选择递归算法。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 模块    \n",
    "> 模块也是用Python写的，包括变量、函数、类及对象等，编写好某特定功能的模块后，以py文件形式存储，文件名就是模块的名称。\n",
    "* 模块实际上是将一组函数放在一起共享公共的主题；\n",
    "* 将这些函数存储于一个.py文件中；\n",
    "* 使用import命令导入。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 标准模块"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.sys模块: 能够通过其访问与Python解释器紧密相关的变量和函数。      \n",
    "2.os模块:这个模块提供了访问多个操作系统服务的功能。     \n",
    "3.fileinput模块:对文本文件进行操作。     \n",
    "4.Random模块:包括随机返回随机数的函数。     \n",
    "........."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 模块调用方式:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.直接调用 import ×××    \n",
    "2.from ××× import ×××  (从 ××× 模块中调用 ××× 功能)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "279.273px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
